#!/usr/bin/env node
 
/**
 * Module dependencies.
 */
 
var program = require('commander');
var task_cpu = require('./commands/cpu');
var task_disk = require('./commands/disk');
var task_process = require('./commands/process');
var _ = require('underscore');
var util = require('util');
var PidFinder = require('./utils/pidFinder');
// var cmdutils = require('./utils/cmdutils');

program
  .version('0.0.1')
  .option('-p, --peppers', 'Add peppers')
  .option('-P, --pineapple', 'Add pineapple')
  .option('-b, --bbq-sauce', 'Add bbq sauce')
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  .parse(process.argv);

  // var pidfile = '/var/run/docker.pid';
  // var r = task_process.processDetect(function(){
  //     var ra = cmdutils.exec("ps -ef|grep nc|grep 9090|awk '{print $2}'").toString().trim().split('\n');
  //     console.log(ra);
  //     if(ra.length == 2){
  //       return parseInt(ra[0].trim());
  //     }else {
  //       return null;
  //     }
  // })
// var pidfile = '/var/run/docker.pid';
// var r = task_process.processDetect(function(){
//   var pf = new PidFinder();
//   return pf.pipe('ps -ef').pipe('grep docker').pipe("grep 'tcp://0.0.0.0:2375'").awk(1).exec();
// });

var r = task_process.processDetect(function(){
  return new PidFinder()
  .pipe('ps -ef')
  .pipe('grep docker')
  .pipe("grep 'tcp://0.0.0.0:2375'")
  .awk().exec();
});
  

// console.log(r);

  // console.log(r);
  // task_cpu.getStatus();
  // var r = task_disk.df();
  // console.log(r);
 
// var banner = task_cpu.getBanner();

// console.log(util.inspect(banner));
// _.each(banner,function (e,i){
//   console.log(e['processor'])
// });

// console.log('you ordered a pizza with:');
// if (program.peppers) console.log('  - peppers');
// if (program.pineapple) console.log('  - pineapple');
// if (program.bbqSauce) console.log('  - bbq');
// console.log('  - %s cheese', program.cheese);


var r = task_cpu.getStatus();

// var cpus = r.cpus;
// for(var i = 0, len = cpus.length; i < len; i++) {
//     console.log("CPU %s:", i);
//     var cpu = cpus[i], total = 0;
//     for(type in cpu.times)
//         total += cpu.times[type];

//     for(type in cpu.times)
//         console.log("\t", type, Math.round(100 * cpu.times[type] / total));
// }

_.each(r.cpus,function(e,i){
  // console.log(e);
  var sum = e.times.user + e.times.sys + e.times.nice + e.times.irq + e.times.idle;
  console.log(sum);
  console.log(e.times.idle);
  console.log(e.times.user);
  console.log('-------------')
  // var cpus =

  // for(var i = 0, len = cpus.length; i < len; i++) {
  //   console.log("CPU %s:", i);
  //   var cpu = cpus[i], total = 0;
  //   for(type in cpu.times)
  //       total += cpu.times[type];

  //   for(type in cpu.times)
  //       console.log("\t", type, Math.round(100 * cpu.times[type] / total));
});
//   console.log(e);
//   // var sum = e.times.user + e.times.sys + e.times.nice + e.times.irq;
//   // console.log(sum);
//   // console.log( ( sum / e.times.idle ) * 100);
//   // console.log('%%us : %d , %%sy : %d',parseInt(e.times.user / sum), parseInt(e.times.sys / sum));
// });